package com.zzyl.serve.service.impl;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDTO;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVO;
import com.zzyl.serve.vo.NursingProjectPlanVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.stream.Collectors;

import static com.zzyl.common.constant.CacheConstants.NURSING_PLAN_ALL_KEY;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;
    @Qualifier("redisTemplate")
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVO selectNursingPlanById(Long id) {
        //查找护理计划基本信息
        NursingPlan plan = getById(id);
        NursingPlanVO projectPlanVO = new NursingPlanVO();
        BeanUtils.copyBeanProp(projectPlanVO, plan);

        //查询护理计划关联信息
        List<NursingProjectPlan> projectPlans = this.nursingProjectPlanService.lambdaQuery()
                .eq(NursingProjectPlan::getPlanId, id)
                .list();

        List<NursingProjectPlanVO> list = projectPlans.stream().map(p -> {
            //新建nursingProjectPlanVO实例
            NursingProjectPlanVO nursingProjectPlanVO = new NursingProjectPlanVO();
            //拷贝
            BeanUtils.copyBeanProp(nursingProjectPlanVO, p);
            //重新赋值
            nursingProjectPlanVO.setExecuteFrequency(String.valueOf(p.getExecuteFrequency()));
            nursingProjectPlanVO.setPlanId(String.valueOf(p.getPlanId()));
            nursingProjectPlanVO.setExecuteCycle(String.valueOf(p.getExecuteCycle()));
            return nursingProjectPlanVO;
        }).collect(Collectors.toList());
        projectPlanVO.setProjectPlans(list);

        return projectPlanVO;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDTO 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDTO nursingPlanDTO) {
        redisTemplate.delete(NURSING_PLAN_ALL_KEY);
        //添加护理计划基本信息
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyBeanProp(nursingPlan, nursingPlanDTO);
        save(nursingPlan);

        Boolean saveResult = null;
        //添加护理计划项目关联表
        List<NursingProjectPlan> projectPlans = nursingPlanDTO.getProjectPlans();
        if (ObjectUtil.isNotEmpty(projectPlans)) {
            projectPlans.forEach(p -> {
                p.setPlanId(nursingPlan.getId());
            });
            //添加新的关联项目信息
            saveResult = this.nursingProjectPlanService.saveBatch(projectPlans);
        }
        return Boolean.TRUE.equals(saveResult) ? 1 : 0;


    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDTO 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDTO nursingPlanDTO) {
        redisTemplate.delete(NURSING_PLAN_ALL_KEY);
        //修改护理计划基本信息
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyBeanProp(nursingPlan, nursingPlanDTO);
        updateById(nursingPlan);

        //添加护理计划关联的项目信息
        Boolean saveResult = true;
        List<NursingProjectPlan> projectPlans = nursingPlanDTO.getProjectPlans();
        //判断集合是否为空
        if (ObjectUtil.isNotEmpty(projectPlans)) {
            //先删除原有的关联项目信息
            this.nursingProjectPlanService.lambdaUpdate()
                    .eq(NursingProjectPlan::getPlanId, nursingPlanDTO.getId())
                    .remove();
            //获取并给每个元素planId进行赋值
            List<NursingProjectPlan> planList = projectPlans.stream().map(projectPlan -> {
                projectPlan.setPlanId(nursingPlan.getId());
                return projectPlan;
            }).collect(Collectors.toList());

            saveResult = this.nursingProjectPlanService.saveBatch(planList);
        }
        return Boolean.TRUE.equals(saveResult) ? 1 : 0;
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids) {
        redisTemplate.delete(NURSING_PLAN_ALL_KEY);
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id) {
        redisTemplate.delete(NURSING_PLAN_ALL_KEY);
        return removeById(id) ? 1 : 0;
    }

    /**
     * 查询所有护理计划
     *
     * @return
     */
    @Override
    public List<NursingPlan> listAll() {
        //从缓存查找数据
        List<NursingPlan> nursingPlans = (List<NursingPlan>) redisTemplate.opsForValue().get(NURSING_PLAN_ALL_KEY);
        //判断是否为空
        if (ObjectUtil.isNotEmpty(nursingPlans)) {
            //不为空则直接返回
            return nursingPlans;
        }
        //不为空，则从数据库查找并存放在缓存中
        LambdaQueryWrapper<NursingPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NursingPlan::getStatus, 1);
        List<NursingPlan> list = this.list(queryWrapper);
        redisTemplate.opsForValue().set(NURSING_PLAN_ALL_KEY, list);
        return list;
    }
}
